package thinkingJava.chapter13;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TheReplacements {
	/*
	 * 		Here's a block of text to use as input to
	 * 		the regular expression matcher. Note that we'll
	 * 		first extract the block of text by looking for
	 *		the special delimiters. then process the
	 *		extracted block. !
	 */
	public static void main(String[] args) {
		String s = "Here's    a block of text to use as input to \n" +
				"the regular expression matcher. Note that we'll \n" +
				" first extract the block of text by looking for \n" +
				" the special delimiters. then process the \n" +
				"extracted block. !";
		String regex = "/\\*!(.*)!\\*/";
		Pattern p = Pattern.compile(regex,Pattern.DOTALL);
		Matcher m = p.matcher(s);
		if (m.find()) {
			s = m.group(1);
		}

		s = s.replaceAll(" {2,}", " ");
		s = s.replaceAll("(?m)^ +", "");
		s = s.replaceAll("(?m) +$", "");
		print(s);
		print("---------------------------------------------");
		s = s.replaceFirst("[aeiou]", "(VOWEL1)");
		print(s);
		print("---------------------------------------------");
		StringBuffer sbuf = new StringBuffer();
		Pattern pa = Pattern.compile("[aeiou]");
		Matcher ma = pa.matcher(s);
		while (ma.find()) {
			ma.appendReplacement(sbuf, ma.group().toUpperCase());
		}
		ma.appendTail(sbuf);
		print(sbuf);
	}
	public static void print(Object o) {
		System.out.println(o);
	}
}
